/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supermarketv1;

import javax.swing.*;
import java.util.*;
import supermarketv0_class.*;

/**
 *
 * @author ΠΑΝΑΓΙΩΤΗΣ v1
 */
public class PurchasingSimulation extends javax.swing.JFrame implements Runnable {

    Thread purchas;
    String namethread = "purchas";
    SuperMarketMainView1 mainscreen;
    DefaultListModel customerModel;
    DefaultListModel cartModel;
    DefaultListModel checkoutModel;
    Customer rand_cust;
    Store rand_stor;
    Product rand_prod;
    ArrayList cart;
    private int counter = 0;

    /**
     * Creates new form PurchasingSimulation
     */
    public PurchasingSimulation(SuperMarketMainView1 mainscreen) {
        initComponents();
        this.mainscreen = mainscreen;
        this.customerModel = new DefaultListModel();
        this.cartModel = new DefaultListModel();
        this.checkoutModel = new DefaultListModel();
        cart = new ArrayList();
        jList1.removeAll();//diagrafo ta panta pao ti lista
        jList2.removeAll();//diagrafo ta panta pao ti lista


        if (purchas == null) {//αν δεν υπάρχει το νημα
            purchas = new Thread(this);//δημιουργία νήματος
            purchas.start();//αρχικη
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList2.setName("jList2"); // NOI18N
        jScrollPane2.setViewportView(jList2);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(supermarketv1.SuperMarketv1App.class).getContext().getResourceMap(PurchasingSimulation.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(0, 217, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                            .addComponent(jScrollPane2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(139, 139, 139)
                        .addComponent(jButton1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addComponent(jButton1)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:Stop/ XMLReport
        purchas.stop();
        mainscreen.db.exportToXML();

        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    public void run() {
        //To change body of generated methods, choose Tools | Templates.
        Random rand = new Random();//για τυχαιο αριθμό
        int delay;
        int rand_cvv;
        while (true) {
            try {
                delay = rand.nextInt(2) + 1;//τυχαιος αριθμός 2-3
                //  Thread.sleep(delay*1000);//καθυστέρηση2-3 sec
                jList1.removeAll();//diagrafo ta panta pao ti lista
                jList2.removeAll();//diagrafo ta panta pao ti lista
                customerModel = new DefaultListModel();
                cartModel = new DefaultListModel();
                checkoutModel = new DefaultListModel();


                //anagnosi  dedomenon
                List<Customer> ListCust = mainscreen.db.readCustomers();
                List<Product> ListProd = mainscreen.db.readProduct();
                List<Store> ListStor = mainscreen.db.readStores();

                rand_cust = ListCust.get(rand.nextInt(ListCust.size() - 1));//πιανω τυχαιο πελάτη
                customerModel.addElement(rand_cust);
                int rand_product_num = rand.nextInt(19) + 1;//τυχαίος αριθμός 1-20
                Thread.sleep(delay * 1000);//καθυστέρηση2-3 sec
                rand_stor = ListStor.get(rand.nextInt(ListStor.size() - 1));//πιανω τυχαιο κατάστημα

                for (int i = 0; i < rand_product_num; i++) {
                    rand_prod = ListProd.get(rand.nextInt(ListProd.size() - 1));//πιάνω τυχαίο προϊόν
                    cartModel.addElement(rand_prod);
                    cart.add(rand_prod);
                }
                jList1.setModel(customerModel);
                jList2.setModel(cartModel);
                // δεύτερο νήμα
                mainscreen.trans = new Transaction(mainscreen, rand_cust, rand_stor, cart, counter++);
                mainscreen.trans.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mainscreen.trans.setVisible(true); //show the form;
                
             

            } catch (InterruptedException e) {
                System.err.println("interrupted1");
            }
        }

    }

    public String getNamethread() {
        return namethread;
    }

    public int getCounter() {
        return counter;
    }
    
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
